/**
 * FlightInfoUtility.java
 * Jan 10, 2010
 */
package com.dlplay.flightinfo.source;

import java.util.List;

import com.dlplay.flightinfo.source.DataSource;

/**
 * @author zhao yu
 *
 */
public class FlightInfoDsUtility {
	
	/*constant values*/
	public static final int FLIGHTINFO_DS_PRIORITY_9 = (9);
	public static final int FLIGHTINFO_DS_PRIORITY_8 = (8);
	public static final int FLIGHTINFO_DS_PRIORITY_7 = (7);
	public static final int FLIGHTINFO_DS_PRIORITY_6 = (6);
	public static final int FLIGHTINFO_DS_PRIORITY_5 = (5);
	public static final int FLIGHTINFO_DS_PRIORITY_4 = (4);
	public static final int FLIGHTINFO_DS_PRIORITY_3 = (3);
	public static final int FLIGHTINFO_DS_PRIORITY_2 = (2);
	public static final int FLIGHTINFO_DS_PRIORITY_1 = (1);
	public static final int FLIGHTINFO_DS_PRIORITY_0 = (0);
	
	public static final int FLIGHTINFO_DS_PRIORITY_HIGH = (FLIGHTINFO_DS_PRIORITY_9);
	public static final int FLIGHTINFO_DS_PRIORITY_MIDDLE = (FLIGHTINFO_DS_PRIORITY_5);
	public static final int FLIGHTINFO_DS_PRIORITY_LOW = (FLIGHTINFO_DS_PRIORITY_0);
	
	public static final String[][] locationTable={{"AKU", "阿克苏", "Akesu", "Akesu"},
		{"AAT", "阿勒泰", "Ataile", "Ataile"},
		{"AKA", "安康", "Ankang", "Ankang"},
		{"AQG", "安庆", "Anqing", "Anqing"},
		{"BSD", "保山", "Baoshan", "Baoshan"},
		{"BAV", "包头", "Baotou", "Baotou"},
		{"BHY", "北海", "Beihai", "Beihai"},
		{"PEK", "北京", "Beijing", "Beijing"},
		{"CGQ", "长春", "Changch", "Changchun"},
		{"CSX", "长沙", "Changsha", "Changsha"},
		{"CIH", "长治", "Changzhi", "Changzhi"},
		{"CGD", "常德", "Changde", "Changde"},
		{"CZX", "常州", "Changzh", "Changzhou"},
		{"CHG", "朝阳", "Chaoyang", "Chaoyang"},
		{"CTU", "成都", "Chengdu", "Chengdu"},
		{"CIF", "赤峰", "Chifeng", "Chifeng"},
		{"CKG", "重庆", "Chongq", "Chongqing"},
		{"DAX", "达县", "Daxian", "Daxian"},
		{"DLU", "大理", "Dali", "Dali"},
		{"DLC", "大连", "Dalian", "Dalian"},
		{"DDG", "丹东", "Dandong", "Dandong"},
		{"DIG", "迪庆", "Diqing", "Diqing"},
		{"DNH", "敦煌", "Dunh", "Dunhuang"},
		{"ENH", "恩施", "Enshi", "Enshi"},
		{"FOC", "福州", "Fuzhou", "Fuzhou"},
		{"KOW", "赣州", "Ganzhou", "Ganzhou"},
		{"GOQ", "格尔木", "Germu", "Germu"},
		{"CAN", "广州", "Guangzh", "Guangzhou"},
		{"KWL", "桂林", "Guilin", "Guilin"},
		{"KWE", "贵阳", "Guiyang", "Guiyang"},
		{"HRB", "哈尔滨", "Haerbin", "Haerbin"},
		{"HAK", "海口", "Haikou", "Haikou"},
		{"HLD", "海拉尔", "Hailaer", "Hailaer"},
		{"HGH", "杭州", "Hangzhou", "Hangzhou"},
		{"HFE", "合肥", "Hefei", "Hefei"},
		{"HTN", "和田", "Hetian", "Hetian"},
		{"HEK", "黑河", "Heihe", "Heihe"},
		{"HET", "呼和浩特", "Huheht", "Huheht"},
		{"TXN", "黄山", "Huangsh", "Huangshan"},
		{"HYN", "黄岩", "Huangyan", "Huangyan"},
		{"JIL", "吉林", "Jilin", "Jilin"},
		{"TNA", "济南", "Jinan", "Jinan"},
		{"JMU", "佳木斯", "Jiamusi", "Jiamusi"},
		{"JGN", "嘉峪关", "Jiayg", "Jiayuguan"},
		{"JNZ", "锦州", "Jinzhou", "Jinzhou"},
		{"JJN", "晋江", "Jinj", "Jinjiang"},
		{"JDZ", "景德镇", "Jingdzh", "Jingdezhen"},
		{"JIU", "九江", "Jiujiang", "Jiujiang"},
		{"JZH", "九寨沟", "Jiuzhg", "Jiuzhaigou"},
		{"KHG", "喀什", "Keshi", "Keshi"},
		{"KRY", "克拉玛依", "Kelmy", "Kelmy"},
		{"KCA", "库车", "Kuche", "Kuche"},
		{"KMG", "昆明", "Kunming", "Kunming"},
		{"LXA", "拉萨", "Lasa", "Lasa"},
		{"LHW", "兰州", "Lanzhou", "Lanzhou"},
		{"LJG", "丽江", "Lijiang", "Lijiang"},
		{"LYG", "连云港", "Lianyg", "Lianyungang"},
		{"LYI", "临沂", "Linyi", "Linyi"},
		{"LZH", "柳州", "Liuzhou", "Liuzhou"},
		{"LYA", "洛阳", "Luoyang", "Luoyang"},
		{"LDG", "罗定", "Luoding", "Luoding"},
		{"LZO", "泸州", "Luzhou", "Luzhou"},
		{"MIG", "绵阳", "Mianyang", "Mianyang"},
		{"LUM", "芒市", "Mangshi", "Mangshi"},
		{"MXZ", "梅县", "Meixian", "Meixian"},
		{"MDG", "牡丹江", "Mudanj", "Mudanjiang"},
		{"KHN", "南昌", "Nanchang", "Nanchang"},
		{"NAO", "南充", "Nanchong", "Nanchong"},
		{"NKG", "南京", "Nanjing", "Nanjing"},
		{"NNG", "南宁", "Nanning", "Nanning"},
		{"NTG", "南通", "Nantong", "Nantong"},
		{"NNY", "南阳", "Nanyang", "Nanyang"},
		{"NGB", "宁波", "Ningbo", "Ningbo"},
		{"NDG", "齐齐哈尔", "Qiqihr", "Qiqihaer"},
		{"IQM", "且末", "Qiemo", "Qiemo"},
		{"SHP", "秦皇岛", "Qinhd", "Qinhuangdao"},
		{"TAO", "青岛", "Qingdao", "Qingdao"},
		{"JUZ", "衢州", "Quzhou", "Quzhou"},
		{"SYX", "三亚", "Sanya", "Sanya"},
		{"SHS", "沙市", "Shashi", "Shashi"},
		{"SWA", "汕头", "Shantou","Shantou"},
		{"SHA", "上海", "ShangHai", "Shanghai"},
		{"SHA1", "上海虹桥", "SHSHA", "Shanghaihongqiao"},
		{"PVG", "上海浦东", "SHPVG", "Shanghaipudong"},
		{"SHE", "沈阳", "Shenyang", "Shenyang"},
		{"SZX", "深圳", "Shenzhen", "Shenzhen"},
		{"SJW", "石家庄", "Shijiazh", "Shijiazhuang"},
		{"SYM", "思芋", "Siyu", "Siyu"},
		{"SZV", "苏州", "Suzhou", "Suzhou"},
		{"TCG", "塔城", "Tacheng", "Tacheng"},
		{"TYN", "太原", "Taiyuan", "Taiyuan"},
		{"TSN", "天津", "Tianjin", "Tianjin"},
		{"TGO", "通辽", "Tongliao", "Tongliao"},
		{"WXN", "万州", "Wanzhou", "Wanzhou"},
		{"WEH", "威海", "Weihai", "Weihai"},
		{"WEF", "潍坊", "Weifang", "Weifang"},
		{"WNZ", "温州", "Wenzhou", "Wenzhou"},
		{"HLH", "乌兰浩特", "Wulht", "Wulanhaote"},
		{"URC", "乌鲁木齐", "Wulumuqi", "Wulumuqi"},
		{"WUH", "武汉", "Wuhan", "Wuhan"},
		{"WUX", "无锡", "Wuxi", "Wuxi"},
		{"WUZ", "梧州", "Wuzhou", "Wuzhou"},
		{"WUS", "武夷山", "Wuyishan", "Wuyishan"},
		{"XIY", "西安", "XiAn", "XiAn"},
		{"XIC", "西昌", "Xichang", "Xichang"},
		{"XNN", "西宁", "Xining", "Xining"},
		{"JHG", "西双版纳", "Xishbn", "Xishuangbanna"},
		{"XIL", "锡林浩特", "Xilinht", "Xilinhaote"},
		{"XMN", "厦门", "Xiamen", "Xiamen"},
		{"XFN", "襄樊", "Xiangfan", "Xiangfan"},
		{"XUZ", "徐州", "Xuzhou", "Xuzhou"},
		{"YNT", "烟台", "Yantai", "Yantai"},
		{"YNJ", "延吉", "Yanji", "Yanji"},
		{"YIN", "伊宁", "Yining", "Yining"},
		{"YBP", "宜宾", "Yibin", "Yibin"},
		{"YIH", "宜昌", "Yichang", "Yichang"},
		{"YIW", "义乌", "Yiwu", "Yiwu"},
		{"INC", "银川", "Yinchuan", "Yinchuan"},
		{"ZHA", "湛江", "Zhanj", "Zhanjiang"},
		{"DYG", "张家界", "Zhangjj", "Zhangjiajie"},
		{"ZAT", "昭通", "Zhaotong", "Zhaotong"},
		{"CGO", "郑州", "Zhengzh", "Zhengzhou"},
		{"HSN", "舟山", "Zhoushan", "Zhoushan"},
		{"ZUH", "珠海", "Zhuhai", "Zhuhai"}};
	
	/*
	 * 
	 */
	private static Integer binarySearch(final String key, final int index)
	{
		int startIndex = 0;
		int endIndex = locationTable.length;
		int currentIndex = (startIndex + endIndex)/2;
		String debugString = null;
		String keyString = null;
		
		int comparationValue=0;
		
		for (int i = currentIndex; startIndex != endIndex; i = (startIndex + endIndex)/2)
		{
			debugString = locationTable[i][index].toLowerCase();
			keyString = key.toLowerCase();
			comparationValue = debugString.compareToIgnoreCase(keyString);
			if (comparationValue == 0)
			{
				return i;
			}
			else if(comparationValue > 0)
			{
				if (startIndex < i)
				{
					startIndex = i;//
				}
				else
				{
					// This is beacuse (startIndex + endIndex)/2 always get the small one;
					startIndex++;
				}
			}
			else
			{
				endIndex = i;
			}
		}
		
		return null;
		
	}
	
	/**
	 * 
	 * @param sourceList
	 * @return
	 * Description:
	 */
	public static List<DataSource> sortByPri(List<DataSource> sourceList) {
		// TODO: Sort the DataSource by pri
		return sourceList;
	}
	
	public static String translateLocChToCode(String locationName)
	{
		Integer resultIndex = binarySearch(locationName, 1);
		if (resultIndex == null)
			return null;
		else
			return locationTable[resultIndex][0];
	}
	
	public static String translateLocFullEnToCode(String locationName)
	{
		Integer resultIndex = binarySearch(locationName, 3);
		if (resultIndex == null)
			return null;
		else
			return locationTable[resultIndex][0];
	}
	
	public static String translateLocCodeToFullEn(String code)
	{
		Integer resultIndex = binarySearch(code, 0);
		if (resultIndex == null)
			return null;
		else
			return locationTable[resultIndex][3];
	}
	
	public static String translateLocToCh(String code)
	{
		Integer resultIndex = binarySearch(code, 0);
		if (resultIndex == null)
			return null;
		else
			return locationTable[resultIndex][1];
	}

}
